package com.jt.AOP;

import com.jt.pojo.User;
import com.jt.service.UserServiceImpl;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
@Aspect
public class SpringAOP {
    private static Map<Integer,User> map = new HashMap();

    @Around("execution(* com.jt.service..*.*(..))")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        Object result=null;
        //1.获取目标对象的参数
        Object[] args = joinPoint.getArgs();
        //2.强制类型转换为对象
        User user = (User) args[0];
        //3.得到此user的id标识
        int id = user.getId();
        //4.判断map中是否存在此id
        if (map.containsKey(id)){
            System.out.println(map.get(id));
            System.out.println("AOP缓存执行");
            return map.get(id);
        }else {
            result = joinPoint.proceed();
            map.put(id,user);
            System.out.println("AOP执行目标方法");
        }

        System.out.println("环绕通知执行");
        return result;
    }
}
